package com.hwg.shiliupro.controller;

import com.hwg.shiliupro.finals.Code;
import com.hwg.shiliupro.po.MsgInfo;
import com.hwg.shiliupro.po.R;
import com.hwg.shiliupro.po.SessionList;
import com.hwg.shiliupro.service.MsgInfoService;
import com.hwg.shiliupro.service.SessionListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/msgs")
public class MsgInfoController {
    @Autowired
    private MsgInfoService msgInfoService;
    @Autowired
    private SessionListService sessionListService;
    //消息列表
    @GetMapping("/{sessionId}")
    public R msgList(@PathVariable Integer sessionId){
        SessionList sessionList = sessionListService.selectById(sessionId);
        if(sessionList == null){
            return R.ok();
        }
        Integer fromUserId = sessionList.getUserId();
        Integer toUserId = sessionList.getToUserId();
        List<MsgInfo> msgInfos = msgInfoService.selectMsgList(fromUserId, toUserId);
        //更新已读消息
        msgInfoService.msgRead(fromUserId,toUserId);
        //更新会话里面的未读消息
        sessionListService.delUnReadCount(fromUserId,toUserId);
        return R.ok().code(Code.GET_OK).data(msgInfos);
    }


}
